package com.mjk.manager.gen.impl.interfaces;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.mjk.common.tools.gson.GsonUtils;
import com.mjk.common.util.ObjectUtils;
import com.mjk.common.util.StrUtils;
import com.mjk.manager.api.constns.ApiRequestType;
import com.mjk.manager.api.dto.ApiFieldEntity;
import com.mjk.manager.api.model.ApiModel;
import com.mjk.manager.gen.dto.GenDto;

public class WebServiceGenImpl implements InterfacesGen {


	private final String packageName ;

	public WebServiceGenImpl(){
		this.packageName = this.getClass().getPackage().getName();
	}

	public static final String SOAP_INTRTFACE = """
            package %s;\r
            import javax.jws.WebParam;\r
            import javax.jws.WebService;\r
			import com.mjk.common.web.getway.restful.prototal.ws.soap.annotation.BeeSoap;\r
			import com.mjk.common.web.getway.restful.prototal.ws.soap.annotation.BeeSoapMethod;\r
			import com.mjk.common.web.getway.restful.prototal.ws.soap.annotation.BeeSoapMethod.ResponseType;\r
            @WebService\r
            @BeeSoap(path = "%s")\r
            public interface %s {\r
            @BeeSoapMethod(work = "%s",method =  "%s",reqType = %s ,rspType  = %s)\r
            public String testDrgs(%s);\r
            }""";
	
	@Override
	public Class<?> createClass(GenDto dto) {
		ApiModel api = dto.getModel();
		List<ApiFieldEntity> fields = GsonUtils.json2List(api.getParams(), ApiFieldEntity.class);
		List<String> params = new ArrayList<>();
		for (ApiFieldEntity field : fields) {
			if(!ObjectUtils.isEmpty(field.getField()))
				params.add("@WebParam(name=\"" + field.getField() + "\")String " + field.getField());
		}
		String clazzName = "I" + StrUtils.toUpperCaseFirstOne(api.getApiWork())+UUID.randomUUID().toString().replace("-", "");
		String iterface = String.format(SOAP_INTRTFACE, packageName,api.getUrl(),
				clazzName, api.getApiWork(), api.getApiMethod(),
				ApiRequestType.getType(api.getRequestType()), ApiRequestType.getType(api.getReturnType()),
				StrUtils.join(params, ","));
	
		return dto.getGroovyClassLoader().parseClass(iterface);    
	}

}
